import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:lazy_food/models/note_model.dart';
import 'package:lazy_food/models/recommend_model.dart';
import 'package:lazy_food/util/string+.dart';

class MoreView extends StatefulWidget {
  final RecommendFoodModel? model;

  MoreView({Key? key, this.model}) : super(key: key);
  _MoreViewState createState() => _MoreViewState();
}

class _MoreViewState extends State<MoreView> {
  bool _isFavorite = false;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        NoteModel? note = widget.model?.data.note;
        bool hasNote = note != null;
        return Column(
          children: [
            Stack(
              children: [
                Container(
                  padding: EdgeInsets.only(
                      left: 20, right: 20, bottom: hasNote ? 0 : 20),
                  width: constraints.maxWidth,
                  height: constraints.maxWidth - 40,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: CachedNetworkImage(
                      imageUrl:
                          widget.model?.data.image.url_pattern.toImage() ?? "",
                      fit: BoxFit.fitWidth,
                      placeholder: (context, s) {
                        return Image.asset('assets/images/placeholder.png');
                      },
                    ),
                  ),
                ),
                // 标题
                Container(
                  padding: EdgeInsets.only(left: 40, top: 20, right: 40),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        widget.model?.data.name_adj ?? "",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 20,
                        ),
                      ),
                      Text(
                        widget.model?.data.name ?? "",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 30,
                          fontWeight: FontWeight.bold,
                        ),
                        softWrap: true,
                        maxLines: 3,
                      ),
                    ],
                  ),
                ),
                // 收藏
                Column(
                  children: [
                    SizedBox(
                      width: 100,
                      height: constraints.maxWidth - 115,
                    ),
                    IconButton(
                      isSelected: _isFavorite,
                      onPressed: () {
                        setState(() {
                          _isFavorite = !_isFavorite;
                        });
                      },
                      icon: Icon(
                        _isFavorite ? Icons.favorite : Icons.favorite_outline,
                        color: Colors.white,
                        size: 30,
                      ),
                    )
                  ],
                )
              ],
            ),
            // 评论
            Container(
              height: hasNote ? 190 : 0,
              padding: hasNote
                  ? EdgeInsets.fromLTRB(20, 10, 20, 20)
                  : EdgeInsets.all(0),
              child: hasNote
                  ? Container(
                      decoration: BoxDecoration(
                        color: Colors.white30,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Column(
                        children: [
                          Divider(
                            height: 10,
                          ),
                          Row(
                            children: [
                              VerticalDivider(
                                width: 15,
                              ),
                              ClipRRect(
                                borderRadius: BorderRadius.circular(15),
                                child: CachedNetworkImage(
                                  imageUrl:
                                      note.user.image.url_pattern.toImage(),
                                  width: 30,
                                  height: 30,
                                  fit: BoxFit.cover,
                                ),
                              ),
                              VerticalDivider(
                                width: 5,
                              ),
                              Text(
                                note.user.name,
                                style: TextStyle(color: Colors.white),
                              ),
                              Spacer(),
                              TextButton.icon(
                                onPressed: () {},
                                icon: Icon(
                                  Icons.favorite,
                                  color: Colors.white,
                                ),
                                label: Text(
                                  '${note.n_diggs}',
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                              VerticalDivider(
                                width: 15,
                              ),
                            ],
                          ),
                          Divider(
                            height: 5,
                          ),
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              VerticalDivider(
                                width: 15,
                              ),
                              ClipRRect(
                                borderRadius: BorderRadius.circular(10),
                                child: CachedNetworkImage(
                                  imageUrl: note.image.url_pattern.toImage(),
                                  width: 60,
                                  height: 80,
                                  fit: BoxFit.cover,
                                ),
                              ),
                              VerticalDivider(
                                width: 10,
                              ),
                              Expanded(
                                child: Text(
                                  note.text,
                                  style: TextStyle(color: Colors.white),
                                  maxLines: 4,
                                ),
                              ),
                              VerticalDivider(
                                width: 15,
                              ),
                            ],
                          ),
                        ],
                      ),
                    )
                  : Divider(height: 0),
            ),
          ],
        );
      },
    );
  }
}
